/*
 * @Author: Ryan Huang
 * @Date: 2019-04-17 22:17:14
 * @Description: 随机一亿数据：users表
 */
package main

import (
	"crypto/md5"
	"database/sql"
	"encoding/hex"
	"fmt"
	"strings"
	"time"

	_ "github.com/go-sql-driver/mysql"
)

const (
	driver = "mysql"
	dns    = "root:root@tcp(127.0.0.1:3306)/study?charset=utf8mb4"
)

func main() {
	db, _ := sql.Open(driver, dns)
	var j = 0
	var sqlState = "insert into users (username,password) values "
	valLen := 10000
	var values = make([]string, valLen)
	for i := 0; i < valLen; i++ {
		j++
		createtime := time.Now().Unix()
		password := _md5(fmt.Sprint(j, createtime))
		username := _md5(fmt.Sprint(j, time.Now().UnixNano()))
		values[i] = fmt.Sprintf("('%s','%s')", username, password)
	}
	sqlState = fmt.Sprintf("%s%s", sqlState, strings.Join(values, ","))
	t := time.Now()
	db.Exec(sqlState)
	fmt.Println(time.Since(t).Seconds())
}
func _md5(s string) string {
	h := md5.New()
	h.Write([]byte(s))
	cipherStr := h.Sum(nil)
	return hex.EncodeToString(cipherStr)
}
